//
//  DYWWeiboModel.swift
//  sinaWeibo
//
//  Created by 董艳武 on 16/2/20.
//  Copyright © 2016年 董艳武. All rights reserved.
//

import UIKit

class DYWWeiboModel: NSObject {

    /// 微博创建时间
    var created_at:String?
    
    /// 微博ID
    var id:Int64 = 0
    
    /// 微博信息内容
    var text:String?
    
    /// 微博来源
    var source:String?
    
    /// user 属性
    var user:DYWUserModel?
    
    /// 图片的url 数组属性 声明为可选项
    var imgUrls:[NSURL]?
    
    /// pic_urls 属性
    var pic_urls: [[String : String]]? {
        didSet {
            // 初始化 imgUrls
            imgUrls = [NSURL]()
            // 遍历数组中的字典，并将其转化为 NSURL
            for item in pic_urls! {
                if let urlString = item["thumbnail_pic"]{
                    let str =  urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                    let url = NSURL(string: str)!
                    self.imgUrls!.append(url)
                }
                
            }
        }
    }
    
    /// 转发微博属性
    var retweeted_status:DYWWeiboModel?
    
    /// 构造方法
    init(dict:[String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    /// 设置 user 属性
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            guard let dict = value as? [String:AnyObject] else {
                return
            }
            self.user = DYWUserModel(dict: dict)
            return
        }
        
        if key == "retweeted_status" {
            guard let dict = value as? [String:AnyObject] else {
                return
            }
            self.retweeted_status = DYWWeiboModel(dict: dict)
            return 
        }
        
        super.setValue(value, forKey: key)
    }
    
    /// 过滤信息
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        // MARK:--什么都不做就是过滤信息
    }
}
